#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include "log.hpp"
#include <cstring>
#include "Common.hpp"
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace LogModule;

const std::string dev = "/dev/null";

void Dameon(int nochdir, int noclose)
{
    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);

    if (fork() > 0)
    {
        exit(0);
    }

    setsid();

    if (nochdir == 0)
        chdir("/");

    if (noclose == 0)
    {
        int fd = ::open(dev.c_str(), O_RDWR);
        if(fd<0)
        {
            LOG(LogLevel::FATAL)<<"open "<<dev<<" error";
            exit(OPEN_ERROR);

        }
        else{
            dup2(fd,0);
            dup2(fd,1);
            dup2(fd,2);
            close(fd);

        }

    }
}